#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

//int main()
//{
//    char *str = "helloworld";
//    *str = "H";//不可以被修改，页表访问权限不允许
//    
//    return 0;
//}



//int g_val = 100;
//
//int main()
//{
//    pid_t id = fork();
//    
//    if(id==0)
//    {
//        //子进程
//        int cnt = 5;
//        while(1)
//        {
//            printf("child, pid:%d, ppid:%d, g_val:%d ,&g_val:%p\n",getpid(),getppid(),g_val,&g_val);
//            sleep(1);
//            if(cnt == 0)
//            {
//                g_val = 200;
//                printf("child change g_val: 100->200\n");
//            }
//            cnt--;
//        }
//    }
//    else 
//    {
//        //父进程
//        while(1)
//        {
//            printf("father, pid:%d, ppid:%d, g_val:%d ,&g_val:%p\n",getpid(),getppid(),g_val,&g_val);
//            sleep(1);
//        }
//
//    }
//
//    return 0;
//}





int un_gval;
int init_gval=100;

int main(int argc, char* argv[],char* env[])
{
    //代码区
    printf("code addr: %p\n",main);
    //字符常量区
    const char *str = "hello Linux";
    //*str = 'h';//不能修改因为字符常量区是被写入到代码区的，而代码区不能被修改
    printf("read only char addr: %p\n",str);
    //已初始化全局变量区
    printf("init global value addr: %p\n",&init_gval);
    //静态变量
    //所谓的静态区就是已初始化全局变量区
    static int a ;
    printf("static addr:%p\n",&a);
    
    //已初始化全局变量区
    printf("uninit global value addr: %p\n",&un_gval);
    //堆区
    char *heap1 = (char*)malloc(100);
    char *heap2 = (char*)malloc(100);
    char *heap3 = (char*)malloc(100);
    char *heap4 = (char*)malloc(100);
    char *heap5 = (char*)malloc(100);
    printf("heap1 addr: %p\n",heap1);//向地址增大方向增长
    printf("heap2 addr: %p\n",heap2);
    printf("heap3 addr: %p\n",heap3);
    printf("heap4 addr: %p\n",heap4);
    printf("heap5 addr: %p\n",heap5);
    //栈区
    printf("stack1 addr: %p\n",&heap1);
    printf("stack2 addr: %p\n",&heap2);
    printf("stack3 addr: %p\n",&heap3);
    printf("stack4 addr: %p\n",&heap4);
    printf("stack5 addr: %p\n",&heap5);

    //命令行参数
    int i = 0;
    for(;argv[i];i++)
    {
        printf("argv[%d]:%p\n",i,argv[i]);
    }
    //环境变量
    for(i=0;env[i];i++)
    {
        printf("env[%d]:%p\n",i,env[i]);
    }


    return 0;
}


